package merchants.service.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import merchants.constant.Constants;
import merchants.constant.ErrorCode;
import merchants.dao.MerchantsDao;
import merchants.entity.Merchants;
import merchants.service.IMerchantsService;
import merchants.vo.CreateMerchantResponse;
import merchants.vo.CreateMerchantsRequest;
import merchants.vo.PassTemplate;
import merchants.vo.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * 商户服务接口实现
 */
@Slf4j
@Service
public class MerchantsServiceImpl implements IMerchantsService {

    /** merchants 数据库接口 */
    private final MerchantsDao merchantsDao;
    /** kafka客户端 */
    private final KafkaTemplate<String ,String> kafkaTemplate;
    @Autowired
    public MerchantsServiceImpl(MerchantsDao merchantsDao, KafkaTemplate<String, String> kafkaTemplate){
        this.merchantsDao = merchantsDao;
        this.kafkaTemplate = kafkaTemplate;
    }

    /**
     *   创建商户服务
     * @param request 创建商户请求 {@link CreateMerchantsRequest}
     * @return {@link Response}
     */
    @Override
    @Transactional
    public Response createMerchants(CreateMerchantsRequest request) {

        //定义返回对象
        Response response = new Response();
        CreateMerchantResponse merchantResponse = new CreateMerchantResponse();

        //校验用户请求是否正确
        ErrorCode errorCode = request.validate(merchantsDao);
        if (errorCode != ErrorCode.SUCCESS) {
            //返回错误信息
            merchantResponse.setId(-1);
            response.setErrCode(errorCode.getCode());
            response.setErrMsg(errorCode.getDesc());
        }else {
            //正确的话保存商户id
            merchantResponse.setId(merchantsDao.save(request.toMerchants()).getId());
        }
        //设置返回数据
        response.setData(merchantResponse);
        return response;
    }

    /**
     * 根据id构造商户信息
     * @param id 商户id
     * @return {@link Response}
     */
    @Override
    public Response buildMerchantsInfoById(Integer id) {

        Response response = new Response();
        Optional<Merchants> merchants = merchantsDao.findById(id);
        //判断是否有该商户信息
        if(!merchants.isPresent()){
            response.setErrCode(ErrorCode.MERCHANTS_NOT_EXIT.getCode());
            response.setErrMsg(ErrorCode.MERCHANTS_NOT_EXIT.getDesc());
        }
        response.setData(merchants);
        return response;
    }

    /**
     * 投放优惠券
     * @param template {@link PassTemplate}
     * @return {@link Response}
     */
    @Override
    public Response dropPassTemplate(PassTemplate template) {

        Response response = new Response();
        ErrorCode errorCode = template.validate(merchantsDao);

        //验证是否有此商户
        if(errorCode != ErrorCode.SUCCESS){
            response.setErrCode(errorCode.getCode());
            response.setErrMsg(errorCode.getDesc());
        }
        else {
            String passTemplate = JSON.toJSONString(template);
            //kafka投放
            kafkaTemplate.send(
                    Constants.TEMPLATE_TOPIC,//kafka topic
                    Constants.TEMPLATE_TOPIC,//kafka key
                    passTemplate //value
            );
            log.info("DropPassTemplates:{}",passTemplate);
        }
        return response;
    }
}
